package com.youdoneed.base.application;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 统一返回消息的格式，其JSON用于接口格式。
 *
 * @author Ewing
 */
@ApiModel("返回结果")
public class ResultMessage<T> {
    // 默认为成功 失败大多表现为异常
    @ApiModelProperty("结果码：0.失败 1.成功 其他值由具体业务而定")
    private int code = 1;
    @ApiModelProperty("处理是否成功")
    private boolean success = true;
    @ApiModelProperty("可读的消息描述")
    private String message = RequestMessage.getMessage("Success");
    // 返回数据可以是任意对象
    @ApiModelProperty("具体数据，若失败则为开发人员可读的异常信息")
    private T data;

    public ResultMessage() {
    }

    public ResultMessage(T data) {
        this.data = data;
    }

    public ResultMessage<T> toSuccess() {
        this.code = 1;
        this.success = true;
        this.message = RequestMessage.getMessage("Success");
        return this;
    }

    public ResultMessage<T> toFailure() {
        this.code = 0;
        this.success = false;
        this.message = RequestMessage.getMessage("Failure");
        return this;
    }

    public ResultMessage<T> toSuccess(String message) {
        this.code = 1;
        this.success = true;
        this.message = RequestMessage.getMessage(message == null ? "Success" : message);
        return this;
    }

    public ResultMessage<T> toFailure(String message) {
        this.code = 0;
        this.success = false;
        this.message = RequestMessage.getMessage(message == null ? "Failure" : message);
        return this;
    }

    public int getCode() {
        return code;
    }

    public ResultMessage<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public ResultMessage<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResultMessage<T> setMessage(String message) {
        this.message = RequestMessage.getMessage(message);
        return this;
    }

    public T getData() {
        return data;
    }

    public ResultMessage<T> setData(T data) {
        this.data = data;
        return this;
    }
}
